package POS.Interface;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A POS System for a
 * small restaurant.
 * 
 * Maeghan Colvin and Jennifer Haveman
 * CSC 289 0B01
 * February 22, 2014
 */
public class ClockInOutPanel extends JPanel
{
    // Variables
    private JPanel mainPanel;                               // Holds mainPanel
    private JPanel numberPanel;                             // Holds keypad Panel
    private JPasswordField employeeNumberPasswordField;     // Censored TextField
    private JButton numbersButton[] = new JButton[9];       // Number buttons 1-8
    private JButton okayButton, zeroButton, clearButton;    // Ok, 0, clear buttons
    private String employeeNumber;                          // Employee ID Number
    private int employeeNumberInt;                          // Employee ID Number int
    private MainFrame MainFrameSQL;                         // SQL Statement for DBMS

    /**
     * Constructor
     */
    public ClockInOutPanel()
    {
        // Build and add mainPanel
        buildPanel();
        this.add(mainPanel);
    }
    
    /**
     * The buildPanel method builds the 
     * Clock In/Out screen.
     */
    
     private JPanel buildPanel()
     {
        // Set layout and border for mainPanel
        mainPanel = new JPanel(new BorderLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 25));
        
        // Create TextField
        employeeNumberPasswordField = new JPasswordField(3);
        //employeeNumberTextField.setEditable(false); // Sets textfield to read-only
        employeeNumberPasswordField.setToolTipText("Enter employee number.");
        employeeNumberPasswordField.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Set layout and border for numberPanel
        numberPanel = new JPanel(new GridLayout(4, 2));
        numberPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        
        // Add buttons to numberPanel
        for(int index = 0; index < numbersButton.length; index++)
        {
            numbersButton[index] = new JButton();
            numbersButton[index].setText(Integer.toString(index + 1));
            numbersButton[index].addActionListener(new employeeNumberActionListener());
            numberPanel.add(numbersButton[index]);
        }
        
        // Add the ok, zero, and clear buttons to the numbersPanel
        okayButton = new JButton("OK");
        okayButton.setMnemonic(KeyEvent.VK_O);
        okayButton.addActionListener(new okayButtonActionListener());
        zeroButton = new JButton("0");
        zeroButton.addActionListener(new employeeNumberActionListener());
        clearButton = new JButton("Clear");
        clearButton.setMnemonic(KeyEvent.VK_C);
        clearButton.addActionListener(new clearActionListener());
        numberPanel.add(okayButton);
        numberPanel.add(zeroButton);
        numberPanel.add(clearButton);
        
        // Add components to mainPanel
        mainPanel.add(employeeNumberPasswordField, BorderLayout.NORTH);
        mainPanel.add(numberPanel, BorderLayout.CENTER);
        
        // Add mainPanel
        this.add(mainPanel, BorderLayout.CENTER);
        
        // Return mainPanel
        return mainPanel;
     }
     
     /**
     * The stringToInt
     * @param strings
     * @return 
     */
    
    public Integer stringToInt(String string)
    {
        employeeNumberInt = Integer.parseInt(employeeNumber);
        if (employeeNumberInt >= 100 && employeeNumberInt <= 999)
        {
            //employeeNumber = employeeNumberPasswordField.getPassword();
            employeeNumberInt = Integer.parseInt(employeeNumber);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Invalid ID Number.");
        }
        System.out.print(employeeNumber);
        return employeeNumberInt;
    }
     
     /**
      * The employeeNumberActionListener displays the numbers
      * from the number pad and displays them in the JPasswordField.
      */
    
     private class employeeNumberActionListener implements ActionListener 
     {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource() == numbersButton[0])  
            {
                String text = employeeNumberPasswordField.getText();
                //char[] employeeNumber = employeeNumberPasswordField.getPassword();
                employeeNumberPasswordField.setText(text + 1);  
            } 
            if(e.getSource() == numbersButton[1])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 2);  
            }
            if(e.getSource() == numbersButton[2])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 3);  
            } 
            if(e.getSource() == numbersButton[3])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 4);  
            }
            if(e.getSource() == numbersButton[4])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 5);  
            }
            if(e.getSource() == numbersButton[5])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 6);  
            }
            if(e.getSource() == numbersButton[6])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 7);  
            }
            if(e.getSource() == numbersButton[7])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 8);  
            }
            if(e.getSource() == numbersButton[8])  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 9);  
            }
            if(e.getSource() == zeroButton)  
            {
                String text = employeeNumberPasswordField.getText();
                employeeNumberPasswordField.setText(text + 0);  
            }
        }
     }
     
     /**
      * The okayButtonActionListener determines the option
      * selected and if the employee number is valid.
      */
     
     private class okayButtonActionListener implements ActionListener 
     {
        @Override
        public void actionPerformed(ActionEvent e) 
        {
            // Retrieves employeeNumber from PasswordField.
            employeeNumber = employeeNumberPasswordField.getText();
            employeeNumberInt = stringToInt(employeeNumber);
            
            System.out.print(employeeNumber);
            
            // Send the statement to the DBMS.
            //results = EateryDBConnection.getTableResults(MainFrameSQL.databaseStatement());
            
            // Clears the PasswordField and resets the focus.
            employeeNumberPasswordField.setText("");
            employeeNumberPasswordField.requestFocus();
        }
     }
     
     /**
      * The clearActionListener clears the textField
      */
     
     private class clearActionListener implements ActionListener 
     {
        @Override
        public void actionPerformed(ActionEvent e) 
        { 
            // Clears the TextField and resets the focus.
            employeeNumberPasswordField.setText("");
            employeeNumberPasswordField.requestFocus();
        }
     }
}
